#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Library for common used functions
'''

import sys
import os

def list_choice(list_args):
    '''
    The same idea as for choice function, but we play only with simple list, without choices
    '''
    
    if len(list_args) == 0:
        raise 'Zero variants. No choice'
    elif len(list_args) == 1:
        return [ list_args[0] ]
    else:
        for i, case in enumerate( list_args ):
            print i,  ' - ',  case
        caseNumber = raw_input('Enter number of the case we are interested in\n')
        try:
            caseNumber = [ int(caseNumber) ]
        except:
            caseNumber = [ int(j) for j in caseNumber.strip().split() ]

        return [ list_args[j] for j in caseNumber ]  
        

def choice(path):
    '''
    path - path to the directory in which we are interested in. Function analyzes what subdirectories are in our directory
    and if there is more then one, asks user in which of them we are interested. As a result we have path to subdirectory    
    '''
    fList = os.listdir( path )
    if len(fList) == 0:
        raise 'Zero variants. No choice'
    elif len(fList) == 1:
        return fList[0]
    else:
        for i, case in enumerate( fList ):
            print i,  ' - ',  case
        
        tt = raw_input('Enter number of the case we are interested in\n')
        try:
            caseNumber = int( tt )
            return fList[ caseNumber]
        except:
            caseNumber = tt.strip().split()
            return [ fList[ int( ttt) ] for ttt in caseNumber ]
        
def argument(list_args):
    '''
    if list_args in sys.argv returns argument of list_args
    else returns False
    '''
    if list_args in sys.argv:
        try:
            return sys.argv[ sys.argv.index(list_args) + 1 ]
        except:
            return True
    else:
        return False
